<template>
  <div class="liveSet">
    <div class="form-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>活动管理</el-breadcrumb-item>
        <el-breadcrumb-item>活动奖品</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="table-bar">
      <div>
        <el-button
          type="primary"
          style="margin-right: 20px"
          size="medium"
          @click="handleAdd"
          >添加奖品</el-button
        >
        <span>卖场：</span>
        <el-select
          v-model="marketId"
          clearable
          @change="selectMarket"
          filterable
          placeholder="请选择卖场"
        >
          <el-option
            v-for="item in marketList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </div>
      <div class="inputSearch">
        <el-input
          placeholder="输入编号搜索"
          v-model="keyword"
          clearable
          @input="clearInput"
          @keyup.enter.native="searchInput"
        ></el-input>
        <el-button type="primary" size="medium" @click="searchInput"
          >搜索</el-button
        >
      </div>
    </div>
    <div class="partnerList_table">
      <div class="partnerList_bottom_table">
        <table>
          <tr>
            <th></th>
            <th>序号</th>
            <th>奖品名称</th>
            <th>奖品主图片</th>
            <th>奖品价值</th>
            <th>库存</th>
            <th>已领取数量</th>
            <th>是否核销奖品</th>
            <th>是否可以购买</th>
            <th>奖品类型</th>
			<th>核销配置</th>
			<!-- <th>赠送抽奖次数</th> -->
            <th>开始时间</th>
            <th>结束时间</th>
            <th>状态</th>
			 <th>排序</th>
            <th>所属卖场</th>
            <th>操作</th>
          </tr>
          <tr v-for="(v, i) in tableList" :key="i">
            <td>
              <font
                class="checkbox"
                :class="{ check_active: checkBoxAll.includes(i) }"
                @click="handlerCheckbox(i)"
                ><i class="el-icon-check"></i
              ></font>
            </td>
            <td>{{ v.id }}</td>
            <td style="max-width: 180px">{{ v.name }}</td>
            <td>
              <el-image
                v-if="v.img"
                style="width: 80px; height: 80px; margin-top: 20px"
                :src="v.img"
                :preview-src-list="[v.img]"
              >
              </el-image>
              <!-- <img style="max-width:200px;max-height:100px;" :src="v.img" alt=""/> -->
            </td>
            <td>{{ v.price }}</td>
            <td>{{ v.stock }}</td>
            <td>{{ v.receive }}</td>
            <td>{{ v.is_verification == 1 ? "支持" : "不支持" }}</td>
            <td>{{ v.ispus == 1 ? "购买可得" : "赠送" }}</td>
            <td>
              {{
                v.type == 0
                  ? "默认（虚拟物品）"
                  : v.type == 1
                  ? "线上优惠券"
                  : v.type == 2
                  ? "实物"
                  : v.type == 3
                  ? "促销文章"
                  : v.type == 4
                  ? "直播促销券":
				  v.type == 5
				  ? "微信红包"
                  : ""
              }}
            </td>
			 <td>{{ v.verif_conf_status==0?"关闭":v.verif_conf_status==1?'开启':'' }}</td>
			 
			 <!-- <td>{{ v.verif_conf_data.lucky_count }}</td> -->
            <td>
              {{ $moment(v.start_at * 1000).format("yyyy-MM-DD HH:mm:ss") }}
            </td>
            <td>
              {{ $moment(v.end_at * 1000).format("yyyy-MM-DD HH:mm:ss") }}
            </td>
            <!-- <td>{{v.created_at}}</td> -->
            <td>{{ v.status == 1 ? "开启" : "关闭" }}</td>
			 <td>{{ v.sort }}</td>
            <td>{{ v.market_name }}</td>
            <td>
              <button
                class="editor"
                style="margin: 0"
                :class="{ delete: v.status == 1 }"
                @click="toChange(v.id, v.status)"
              >
                {{ v.status == 1 ? "关闭" : "开启" }}
              </button>
              <!-- <button class="yellow" @click="toDel(v.id)">删除</button> -->
              <button class="shelves" @click="toEdit(v.id)">编辑</button>
              <button
                class="yellow"
                @click="toWriteOff(v.name, v.id, v.market_id, v.market_name)"
              >
                核销码列表
              </button>
            </td>
          </tr>
          <tr v-if="tableList.length == 0">
            <td class="table_title" colspan="20">当前数据空空如也</td>
          </tr>
        </table>
        <div class="partnerList_bottom_page">
          <div class="partnerList_bottom_page_left">
            <div
              class="partnerList_bottom_page_left_checkAll"
              @click="handerCheckAll"
            >
              <font class="checkbox" :class="{ check_active: checkAll }"
                ><i class="el-icon-check"></i
              ></font>
            </div>
            全选
            <!-- <button class="partnerList_bottom_page_left_btn" @click="selectDel">删除</button> -->
          </div>
          <div class="partnerList_bottom_page_right">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              background
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      checkAll: false,
      page: 1,
      pageSize: 10,
      total: 0,
      checkBoxAll: [], // 选中的数据
      shelvesAlls: [],
      tableList: [],
      keyword: "",
      code: "",
      activity_id: "", //主活动id
      fatherName: "", //主活动名称
      marketId: "", //卖场id
      marketList: [],
    };
  },
  mounted() {
    this.activity_id = this.$route.query.id;
    this.fatherName = this.$route.query.name;
    this.getSetList();
    this.remoteMethod();
  },
  methods: {
    // 获取卖场列表
    remoteMethod() {
      this.$http.post("/api/root/Market/marketList").then((res) => {
        console.log(res, "卖场列表");
        if (res.code == 1) {
          this.marketList = res.data ? res.data : [];
        }
      });
    },
    // 选择卖场
    selectMarket() {
      this.page = 1;
      this.pageSize = 10;
      this.getSetList();
    },
    // 获取活动奖品
    getSetList() {
      let params = {
        keyword: this.keyword,
        page: this.page,
        pagesize: this.pageSize,
        market_id: this.marketId,
      };
      this.$http.post("/api/root/activity/prize/lists", params).then((res) => {
        console.log(res, "活动列表");
        if (res.code == 1) {
          this.tableList = res.data.data ? res.data.data : [];
          this.total = res.data.count;
        }
      });
    },

    // 搜索
    searchInput() {
      if (this.keyword != "") {
        this.page = 1;
        this.pageSize = 10;
        this.getSetList();
      }
    },
    // 清空输入框
    clearInput() {
      if (this.keyword == "") {
        this.page = 1;
        this.pageSize = 10;
        this.getSetList();
      }
    },

    // 添加活动按钮
    handleAdd() {
      this.$router.push({ name: "activityPrizeAdd", query: {} });
    },

    // 删除按钮
    toDel(id) {
      this.$confirm("此操作将删除此该活动, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http.post("/api/root/activity/son/del", { id }).then((res) => {
          console.log(res);
          if (res.code == 1) {
            this.getSetList();
            this.$message({
              type: "success",
              message: "删除成功",
            });
          } else {
            this.$message.warning(res.msg);
          }
        });
      });
    },

    // 编辑按钮
    toEdit(id) {
      this.$router.push({ name: "activityPrizeAdd", query: { id } });
    },

    // 进入核销码列表
    toWriteOff(name, id, market_id, market_name) {
      this.$router.push({
        name: "writeOffList",
        query: { name, id, market_id, market_name },
      });
    },

    // 更改状态
    toChange(id, sta) {
      let params = {
        id,
        status: sta == 1 ? 0 : 1,
      };
      this.$http.post("/api/root/activity/prize/status", params).then((res) => {
        console.log(res, "更改状态");
        if (res.code == 1) {
          this.$message.success("更改成功");
          this.getSetList();
        } else {
          this.$message.warning(res.msg);
        }
      });
    },

    // 多选
    handlerCheckbox(i) {
      console.log(i);
      let arr = this.checkBoxAll;
      if (arr.includes(i)) {
        let index = arr.indexOf(i);
        if (index != -1) {
          arr.splice(index, 1);
          this.shelvesAlls.splice(index, 1);
          if (this.checkBoxAll.length !== this.tableList.length) {
            this.checkAll = false;
          }
        }
      } else {
        this.checkBoxAll.push(i);
        this.shelvesAlls.push(this.tableList[i].id);
        if (this.checkBoxAll.length == this.tableList.length) {
          this.checkAll = true;
        }
      }
      console.log(this.shelvesAlls);
    },
    // 全选与全不选
    handerCheckAll() {
      this.checkAll = !this.checkAll;
      let arr = this.tableList.length;
      this.checkBoxAll = [];
      this.shelvesAlls = [];
      if (this.checkAll) {
        for (let i = 0; i < arr; i++) {
          this.checkBoxAll.push(i);
        }
        this.checkBoxAll.map((v) => {
          this.shelvesAlls.push(this.tableList[v].id.toString());
        });
      } else {
        this.checkBoxAll = [];
        this.shelvesAlls = [];
      }
      console.log(this.shelvesAlls);
    },

    //更改直播配置
    // toChange(id,sta){
    //     let status = sta==0?1:0;
    //     this.$http.post('/api/root/SystemConfig/changeStatus',{id,status}).then(res=>{
    //         console.log(res,'直播配置结果');
    //         if(res.code==1){
    //             this.$message.success('操作成功')
    //             this.getSetList()
    //         }else{
    //             this.$message.warning(res.msg)
    //         }
    //     })
    // },

    handleSizeChange(val) {
      this.page = 1;
      this.pageSize = val;
      this.getSetList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getSetList();
    },
  },
};
</script>
<style lang="less" scoped>
@import "index.less";
</style>